#!/usr/bin/env python

# Copyright (c) 2009, Purdue University
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# 
# Neither the name of the Purdue University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Recovery tool for Roster"""


__copyright__ = 'Copyright (C) 2009, Purdue University'
__license__ = 'BSD'
__version__ = '0.17'


import sys
from optparse import OptionParser
import ConfigParser
import getpass

import roster_core
from roster_config_manager import db_recovery

def main(args):
  """Collects command line arguments. Exports tree.

  Inputs:
    args: list of arguments from the command line
  """
  usage = ('\n'
           '\n'
           'To recover up to a certian audit ID:\n'
           '\t%s -i <id>\n'
           '\n'
           'To recover a single audit step:\n'
           '\t%s -i <id> --single\n' % tuple(
             [sys.argv[0] for _ in range(2)]))

  parser = OptionParser(version='%%prog (Roster %s)' % __version__, usage=usage)

  parser.add_option('-i', '--id', dest='id', action='store',
                    help='Audit log ID to recover to.',
                    metavar='<id>', default=None)
  parser.add_option('--single', dest='single', action='store_true',
                    help='Run single audit rather than a full range.',
                    default=False)
  parser.add_option('-u', '--username', action='store', dest='username',
                    help='Run as a different username.', metavar='<username>',
                    default=unicode(getpass.getuser()))
  parser.add_option('--config-file', action='store', dest='config_file',
                    help='Config file location.', metavar='<file>',
                    default=None)

  (globals()["options"], args) = parser.parse_args(args)
  
  server_config_file = ConfigParser.SafeConfigParser()

  server_config_file.read(options.config_file)

  options.username = unicode(options.username)
  options.id = int(options.id)
  config_instance = roster_core.Config(file_name=options.config_file)
  recovery_instance = db_recovery.Recover(options.username, config_instance)


  if( not options.id ):
    print 'ERROR: An audit log ID must be specified to recover Roster.'
    sys.exit(1)

  if( options.single ):
    recovery_instance.RunAuditStep(options.id)

  else:
    recovery_instance.RunAuditRange(options.id)

if __name__ == "__main__":
  main(sys.argv[1:])